import 'package:flutter/material.dart';
import 'package:flutter_marktrip/pages/HomePage.dart';
import 'package:flutter_marktrip/pages/MyPage.dart';
import 'package:flutter_marktrip/pages/SearchPage.dart';
import 'package:flutter_marktrip/pages/TravelPage.dart';

/**
 * 首页tab页
 */
class TabNavigator extends StatefulWidget {
  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;
  int _currentIndex = 0;
  final PageController _pageController = new PageController(
    initialPage: 0,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        children: [
          HomePage(),
          SearchPage(),
          TravelPage(),
          MyPage(),
        ],
        controller: _pageController,
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          _pageController.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        type: BottomNavigationBarType.fixed,
        items: [
          _BottomItem('首页', Icons.home, 0),
          _BottomItem('搜索', Icons.search, 1),
          _BottomItem('旅拍', Icons.camera_alt, 2),
          _BottomItem('我的', Icons.account_circle, 3)
        ],
      ),
    );
  }

  //tab按钮
  _BottomItem(String title, IconData icon, int index) {
    return BottomNavigationBarItem(
        title: Text(
          title,
          style: TextStyle(
            color: _currentIndex == index ? _activeColor : _defaultColor,
          ),
        ),
        icon: Icon(
          icon,
          color: _defaultColor,
        ),
        activeIcon: Icon(
          icon,
          color: _activeColor,
        ));
  }
}
